class IPromise {
    constructor(cb) {
        this.initValue();
        this.initBind();
        try {
            cb(this.resolve, this.reject);
        } catch (err) {
            this.reject(err);
        }
    }
    initValue() {
        this.PromiseState = 'pending';
        this.PromiseResult = null;
        this.onFulfilledCallbacks = [] // 保存成功回调
        this.onRejectedCallbacks = [] // 保存失败回调
    }
    initBind() {
        this.resolve = this.resolve.bind(this);
        this.reject = this.reject.bind(this);
    }
    resolve(value) {
        if (this.PromiseState !== 'pending') {
            return;
        }
        this.PromiseState = 'fulfilled';
        this.PromiseResult = value;
        // 执⾏保存的成功回调
        while (this.onFulfilledCallbacks.length) {
            this.onFulfilledCallbacks.shift()()
        }
    }
    reject(value) {
        if (this.PromiseState !== 'pending') {
            return;
        }
        this.PromiseState = 'rejected';
        this.PromiseResult = value;
        // 执⾏保存的失败回调
        while (this.onRejectedCallbacks.length) {
            this.onRejectedCallbacks.shift()()
        }
    }
    then(onFulfilled, onRejected) {
        // 参数校验，确保⼀定是函数
        const thenPromise = new IPromise((resolve, reject) => {
            onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : resolve;
            onRejected = typeof onRejected === 'function' ? onRejected : reject;

            if (this.PromiseState === 'fulfilled') {
                // 如果当前为成功状态，执⾏第⼀个回调
                setTimeout(()=>{
                    this.resolvePromise(thenPromise, onFulfilled, resolve, reject)
                },0)
            } else if (this.PromiseState === 'rejected') {
                // 如果当前为失败状态，执⾏第⼆个回调
                setTimeout(()=>{
                    this.resolvePromise(thenPromise, onRejected, resolve, reject)
                },0)
            } else if (this.PromiseState === 'pending') {
                // 如果状态为待定状态，暂时保存两个回调
                setTimeout(()=>{
                    this.onFulfilledCallbacks.push(this.resolvePromise.bind(this, thenPromise, onFulfilled, resolve, reject));
                    this.onRejectedCallbacks.push(this.resolvePromise.bind(this, thenPromise, onRejected, resolve, reject));
                },0)
            }
        })
        // 返回这个包装的Promise
        return thenPromise;
    }
    catch(onRejected) {
        return this.then(undefined, onRejected);
    }
    resolvePromise(promise2, cb, resolve, reject) {
        try {
            const res = cb(this.PromiseResult)
            if (res === promise2) {
                // 不能返回⾃身
                throw new Error('Chaining cycle detected for promise !')
            }
            if (res instanceof IPromise) {
                res.then(resolve,reject)
            } else {
                // ⾮Promise就直接成功
                resolve(res)
            }
        } catch (err) {
            // 处理报错
            reject(err)
            throw new Error(err)
        }
    }
}